package com.example.excel.ui.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.excel.R;
import com.example.excel.base.BaseActivity;
import com.example.excel.bean.InventoryBean;
import com.example.excel.config.Constants;
import com.example.excel.databinding.ActivityMain2Binding;
import com.example.excel.db.InventoryController;
import com.example.excel.ui.activity.local.LocalInfoActivity;
import com.example.excel.ui.activity.query.AutoQueryActivity;
import com.example.excel.ui.activity.query.InventoryActivity;
import com.example.excel.ui.activity.setting.SettingActivity;
import com.example.excel.utils.SharedPreferencesUtils;
import com.example.excel.utils.Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Observer;
import androidx.viewpager.widget.ViewPager;
import me.rosuh.filepicker.config.FilePickerManager;

public class MainActivity2 extends BaseActivity<ActivityMain2Binding, MainViewModel2> {
    int UPLOAD_FILE_RESULT = 6001;

    @Override
    protected Class<MainViewModel2> initViewModel() {
        return MainViewModel2.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main2;
    }

    @Override
    protected void initView() {
        mBinding.setEvent(new DataBindingMainHandler());
    }

    @Override
    protected void registerObserve() {
        super.registerObserve();
        mViewModel.mShowUI.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Utils.getInstance().showToast(s);
            }
        });
        mViewModel.mLoadSuccess.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    SharedPreferencesUtils.setBoolean(Constants.LOAD_COMPLE, true);
                    new AlertDialog.Builder(MainActivity2.this).setMessage("下载完成，共" + mViewModel.mTotal + "条数据").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).create().show();
                } else {
                    Utils.getInstance().showToast("获取失败！");
                }
            }
        });
        mViewModel.mUploadSuccess.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    SharedPreferencesUtils.setBoolean(Constants.UPLOAD_COMPLE, true);
                    new AlertDialog.Builder(MainActivity2.this).setMessage("导出数据成功！").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).create().show();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == UPLOAD_FILE_RESULT) {
            if (resultCode == Activity.RESULT_OK) {
                List<String> list = FilePickerManager.INSTANCE.obtainData();
                Log.d("ddd", "path:" + list.get(0));
                mViewModel.loadInventoryData(list.get(0));
            } else {
                Toast.makeText(this, "没有选择任何东西~", Toast.LENGTH_SHORT).show();
            }
        }

    }

    private void showFinishDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确定删除本地数据吗？");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mViewModel.clear();
            }
        });
        builder.show();
    }

    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("本地有未提交的数据，确定删除本地数据吗？");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mViewModel.clear();
            }
        });
        builder.show();
    }

    private void showDeleteDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("本地有未提交的数据，是否删除本地数据，重新下载？");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mViewModel.clear();
            }
        });
        builder.show();
    }

    public class DataBindingMainHandler {
        // 资料下载
        public void onDownloadInfo(View view) {
            if (SharedPreferencesUtils.getBoolean(Constants.LOAD_COMPLE, false)) {
                showDeleteDialog();
                return;
            }
            FilePickerManager.INSTANCE.from(MainActivity2.this)
                    .forResult(UPLOAD_FILE_RESULT);
        }

        // 盘点
        public void onInventoryInfos(View view) {
            if (!SharedPreferencesUtils.getBoolean(Constants.LOAD_COMPLE, false)) {
                Utils.getInstance().showToast("请先下载盘点资料");
                return;
            }
//            InventoryActivity.start(view.getContext());
            AutoQueryActivity.start(view.getContext());
        }

        // 上传数据
        public void onUploadInfo(View view) {
            if (!SharedPreferencesUtils.getBoolean(Constants.LOAD_COMPLE, false)) {
                Utils.getInstance().showToast("请先下载盘点资料");
                return;
            }
            /*if (SharedPreferencesUtils.getBoolean(Constants.UPLOAD_COMPLE, false)) {
                Utils.getInstance().showToast("盘点已完成，不能重复提交");
                return;
            }*/
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity2.this);
            builder.setTitle("是否结束该盘点单？");
            builder.setNegativeButton("取消", null);
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String mCurFilePath = SharedPreferencesUtils.getString(Constants.EXCEL_FILE_PATH, "");
                    File file = new File(mCurFilePath);
                    String name = file.getParentFile().getAbsolutePath() + File.separator + System.currentTimeMillis() + "_" + file.getName();
                    mViewModel.uploadInventoryData(name);
                }
            });
            builder.show();
        }

        // 查询
        public void onQueryInventory(View view) {
            if (!SharedPreferencesUtils.getBoolean(Constants.LOAD_COMPLE, false)) {
                Utils.getInstance().showToast("请先下载盘点资料");
                return;
            }
            LocalInfoActivity.start(view.getContext());
        }

        // 清除
        public void onClear(View view) {
            if (!SharedPreferencesUtils.getBoolean(Constants.LOAD_COMPLE, false)) {
                Utils.getInstance().showToast("请先下载盘点资料");
                return;
            }
            if (!SharedPreferencesUtils.getBoolean(Constants.UPLOAD_COMPLE, false)) {
                showDialog();
            } else {
                showFinishDialog();
            }
        }

        // 设置
        public void onSystemSettings(View view) {
            SettingActivity.start(view.getContext());
        }
    }

}
